{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scannet vizualisation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import os\n",
    "import sys\n",
    "import panel as pn\n",
    "import numpy as np\n",
    "import pyvista as pv\n",
    "import glob\n",
    "from matplotlib.colors import ListedColormap\n",
    "from omegaconf import OmegaConf\n",
    "\n",
    "pn.extension('vtk')\n",
    "os.system('/usr/bin/Xvfb :99 -screen 0 1024x768x24 &')\n",
    "os.environ['DISPLAY'] = ':99'\n",
    "os.environ['PYVISTA_OFF_SCREEN'] = 'True'\n",
    "os.environ['PYVISTA_USE_PANEL'] = 'True'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DIR = os.path.dirname(os.getcwd())\n",
    "ROOT = os.path.join(DIR, \"..\")\n",
    "sys.path.insert(0, ROOT)\n",
    "sys.path.insert(0, DIR)\n",
    "from torch_points3d.datasets.segmentation.s3dis import S3DISSphere, S3DISFusedDataset\n",
    "from torch_points3d.datasets.segmentation import IGNORE_LABEL"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load S3DIS dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DIR = os.path.dirname(os.getcwd())\n",
    "dataset_options = OmegaConf.load(os.path.join(DIR,'conf/data/segmentation/s3disfused.yaml'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_options.data.dataroot = os.path.join(DIR,\"data\")\n",
    "dataset = S3DISFusedDataset(dataset_options.data)\n",
    "print(dataset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualise the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OBJECT_COLOR = [\n",
    "        [233, 229, 107],  #'ceiling' .-> .yellow\n",
    "        [95, 156, 196],  #'floor' .-> . blue\n",
    "        [179, 116, 81],  #'wall'  ->  brown\n",
    "        [241, 149, 131],  #'beam'  ->  salmon\n",
    "        [81, 163, 148],  #'column'  ->  bluegreen\n",
    "        [77, 174, 84],  #'window'  ->  bright green\n",
    "        [108, 135, 75],  #'door'   ->  dark green\n",
    "        [41, 49, 101],  #'chair'  ->  darkblue\n",
    "        [79, 79, 76],  #'table'  ->  dark grey\n",
    "        [223, 52, 52],  #'bookcase'  ->  red\n",
    "        [89, 47, 95],  #'sofa'  ->  purple\n",
    "        [81, 109, 114],  #'board'   ->  grey\n",
    "        [233, 233, 229],  #'clutter'  ->  light grey\n",
    "        [0, 0, 0],  # unlabelled .->. black\n",
    "    ]\n",
    "def buil_cmap():\n",
    "    mapping = np.linspace(0, len(OBJECT_COLOR), 256)\n",
    "    newcolors = np.zeros((256, 3))\n",
    "    for i, color in enumerate(OBJECT_COLOR):\n",
    "        newcolors[mapping >= i-0.5] = np.asarray(color) / 255.\n",
    "    return ListedColormap(newcolors)\n",
    "cmap = buil_cmap()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_random_data(event):\n",
    "    i = np.random.randint(0, len(dataset.train_dataset))\n",
    "    sample = dataset.train_dataset[i]\n",
    "    xyz = sample.pos.numpy()\n",
    "    pl = pv.Plotter(notebook=True)\n",
    "    point_cloud = pv.PolyData(xyz)\n",
    "    point_cloud['label'] = sample.y.numpy()\n",
    "    pl.add_points(point_cloud,cmap=cmap, clim=[0, len(OBJECT_COLOR)]) \n",
    "    \n",
    "    pan.object = pl.ren_win\n",
    "    return point_cloud"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pl = pv.Plotter(notebook=True)\n",
    "pan = pn.panel(pl.ren_win, sizing_mode='scale_both', aspect_ratio=1,orientation_widget=True,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "button = pn.widgets.Button(name='Load new model', button_type='primary')\n",
    "button.on_click(load_random_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dashboard = pn.Row(\n",
    "    pn.Column('## S3DIS visualiser',button),\n",
    "    pan\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dashboard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Validation dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 3\n",
    "area = dataset.val_dataset._datas[i]\n",
    "xyz = area.pos.numpy()\n",
    "pl = pv.Plotter(notebook=True)\n",
    "point_cloud = pv.PolyData(xyz)\n",
    "point_cloud['label'] = area.y.numpy()\n",
    "pl.add_points(point_cloud,cmap=cmap, clim=[0, len(OBJECT_COLOR)]) \n",
    "pl.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 3\n",
    "area = dataset.train_dataset._datas[i]\n",
    "xyz = area.pos.numpy()\n",
    "pl = pv.Plotter(notebook=True)\n",
    "point_cloud = pv.PolyData(xyz)\n",
    "point_cloud['label'] = area.y.numpy()\n",
    "pl.add_points(point_cloud,cmap=cmap, clim=[0, len(OBJECT_COLOR)]) \n",
    "pl.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 3\n",
    "area = dataset.trainval_dataset._datas[i]\n",
    "xyz = area.pos.numpy()\n",
    "pl = pv.Plotter(notebook=True)\n",
    "point_cloud = pv.PolyData(xyz)\n",
    "point_cloud['label'] = area.y.numpy()\n",
    "pl.add_points(point_cloud,cmap=cmap, clim=[0, len(OBJECT_COLOR)]) \n",
    "pl.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
